package com.cl.unipay4j.wx.redpack;

import com.cl.unipay4j.core.Result;
import com.cl.unipay4j.core.UnipayConst;
import com.cl.unipay4j.core.redpack.IRedpackQuery;
import com.cl.unipay4j.core.redpack.RedpackQueryModel;
import com.cl.unipay4j.core.redpack.RedpackQueryResponse;
import com.cl.unipay4j.core.util.MoneyUtil;
import com.cl.unipay4j.wx.AbstractWxPay;
import com.cl.unipay4j.wx.WxBizEnum;
import com.cl.unipay4j.wx.sdk.WXPayConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信红包查询
 *
 * @author  siashan
 * @version  V1.0.1
 **/
@Slf4j
public class WxPayRedpackQuery extends AbstractWxPay implements IRedpackQuery {
    /**
     * 查询红包发放记录
     *
     * @param model   红包查询实体
     * @return        红包发放查询结果
     */
    @Override
    public Result<RedpackQueryResponse> execQuery(RedpackQueryModel model) {
        log.info("WxPayRedpackQuery => 订单{} 微信红包发放记录查询请求到达", model.getOutTradeNo());
        try {
            Map<String, String> r = initWxPay().sendRequest(wrapParam(model), WXPayConstants.MMPAYMKTTRANSFERS_GETHBINFO_URL_SUFFIX, WxBizEnum.REDPACK, true);
            log.info("WxPayRedpackQuery => 订单{} 微信红包发放记录查询请求返回报文:{}", model.getOutTradeNo(),r);
            String returnCode = r.get(WXPayConstants.RETURN_CODE);
            if (UnipayConst.SUCCESS.equals(returnCode)) {
                if (UnipayConst.SUCCESS.equals(r.get(WXPayConstants.RESULT_CODE))) {
                    log.info("WxPayRedpackQuery =>订单{} 微信红包发放记录查询返回成功", model.getOutTradeNo());
                    return Result.tradeSuccess(wrapResp(r));
                } else {
                    String errCode = r.get(WXPayConstants.ERR_CODE);
                    String errCodeDes = r.get(WXPayConstants.ERR_CODE_DES);
                    log.info("WxPayRedpackQuery =>订单{} 微信红包发放记录查询返回失败，错误代码:{},错误描述:{}", model.getOutTradeNo(),errCode,errCodeDes);
                    return Result.returnFail(errCode, errCodeDes);
                }
            } else {
                String returnMsg = r.get(WXPayConstants.RETURN_MSG);
                log.info("WxPayRedpackQuery =>订单{} 微信红包发放记录查询返回失败，错误代码:{},错误描述:{}", model.getOutTradeNo(),returnCode,returnMsg);
                return Result.returnFail(returnCode, returnMsg);
            }

        } catch (Exception e) {
            log.info("WxPayRedpackQuery =>订单{} 微信红包发放记录查询异常", model.getOutTradeNo(),e);
        }
        return Result.exception();
    }


    /**
     * 封装请求参数
     *
     * @param model    请求实体
     * @return         封装后的请求参数
     */
    private Map<String, String> wrapParam(RedpackQueryModel model) {
        Map<String, String> map = new HashMap<>();
        map.put("mch_billno", model.getOutTradeNo());
        map.put("bill_type", "MCHT");

        return map;
    }


    /**
     * 封装红包记录查询返回结果
     *
     * @param r    微信返回报文
     * @return     返回结果
     */
    private RedpackQueryResponse wrapResp(Map<String, String> r){
        RedpackQueryResponse response = new RedpackQueryResponse();
        response.setAmount(MoneyUtil.cent2Yuan(Integer.parseInt(r.get("total_amount"))));
        response.setTotalNum(Integer.parseInt(r.get("total_num")));
        response.setOutTradeNo(r.get("mch_billno"));
        response.setTradeNo(r.get("detail_id"));
        response.setBody(r);
        return response;
    }
}
